package io.github.uicdb.liba.api.register;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import io.github.uicdb.liba.api.register.codec.IngredientCodec;
import io.github.uicdb.liba.api.register.codec.ItemStackCodec;
import io.github.uicdb.liba.api.register.codec.NonnullIngredientListCodec;
import io.github.uicdb.liba.api.register.codec.ResourceLocationCodec;
import net.minecraft.core.NonNullList;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.crafting.Ingredient;
import net.minecraft.world.item.crafting.RecipeSerializer;
import net.minecraftforge.registries.ForgeRegistryEntry;

import javax.annotation.ParametersAreNonnullByDefault;

@ParametersAreNonnullByDefault
@SuppressWarnings("unused")
public abstract class LazyRecipeSerializer<T extends LazyRecipe> extends ForgeRegistryEntry<RecipeSerializer<?>> implements RecipeSerializer<T> {

    protected  final Gson GSON;
    {
        GSON=makeGson(new GsonBuilder()).create();
    }
    protected  GsonBuilder makeGson(GsonBuilder gsonBuilderIn){
          gsonBuilderIn.excludeFieldsWithoutExposeAnnotation()
                .setPrettyPrinting()
                .disableHtmlEscaping()
                .registerTypeAdapter(Ingredient.class,new IngredientCodec())
                .registerTypeAdapter(ItemStack.class,new ItemStackCodec())
                .registerTypeAdapter(ResourceLocation.class,new ResourceLocationCodec())
                .registerTypeAdapter(new TypeToken<NonNullList<Ingredient>>(){}.getType(),new NonnullIngredientListCodec());
          return gsonBuilderIn;
    }
    public JsonObject toJson(T recipe) {
        return (JsonObject) GSON.toJsonTree(recipe);
    }
}
